#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10,M=105;
int n,m,T,a[N],c[N],X[10],dp[N];
char ch;
vector<char>v[M];
struct node{
    int nl,id;
}val[N],tmp[N];
void solve(int i,int sum,int r,node p[N]){
    if(sum==1){
        dp[i]=p[i].id;
        return;
    }
    int idx=0,pk=0;
    for(int i=1;i<=sum;i+=2){
        if(v[r][idx]=='0'){
            if(p[i].nl>=r) tmp[++pk]=p[i];
            else tmp[++pk]=p[i+1];
        }
        else{
            if(p[i+1].nl>=r) tmp[++pk]=p[i+1];
            else tmp[++pk]=p[i];
        }
        idx++;
    }
    solve(i,sum/2,r+1,tmp);
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cin>>c[i];
    int K=ceil(log2(n));
    for(int i=1;i<=K;i++){
        for(int j=1;j<=int(pow(2,K-i));j++){
            cin>>ch;
            v[i].push_back(ch);
        }
    }
    cin>>T;
    while(T--){
        memset(dp,0,sizeof(dp));
        for(int i=0;i<4;i++) cin>>X[i];
        for(int i=1;i<=n;i++){
            val[i].nl=a[i]^X[i%4];
            val[i].id=i;
        }
        int ans=0;
        for(int i=1;i<=m;i++){
            int k=ceil(log2(c[i]));
            solve(i,c[i],k,val);
            ans=ans^(i*dp[i]);
        }
        cout<<ans<<'\n';
    }
    return 0;
}
